import Foundation

public class AppUsageTracker {
    public static let shared = AppUsageTracker()
    
    // 当天使用时长
    private var todayUsageTime: TimeInterval = 0
    
    // 本周使用时长
    private var weeklyUsageTime: TimeInterval = 0
    
    // 本月使用时长
    private var monthlyUsageTime: TimeInterval = 0
    
    // 上次统计的日期
    private var lastRecordDate: Date?
    
    private init() {
        loadUsageData()
    }
    
    // 更新使用时长
    public func updateUsageTime(_ duration: TimeInterval) {
        let currentDate = Date()
        
        // 检查是否需要重置统计数据
        checkAndResetStatistics(currentDate)
        
        // 更新各时间段的使用时长
        todayUsageTime += duration
        weeklyUsageTime += duration
        monthlyUsageTime += duration
        
        // 保存最新的统计数据
        saveUsageData()
        
        // 更新最后记录时间
        lastRecordDate = currentDate
    }
    
    // 检查并重置统计数据
    private func checkAndResetStatistics(_ currentDate: Date) {
        guard let lastDate = lastRecordDate else {
            lastRecordDate = currentDate
            return
        }
        
        let calendar = Calendar.current
        
        // 检查是否是新的一天
        if !calendar.isDate(lastDate, inSameDayAs: currentDate) {
            // 重置当天使用时长
            todayUsageTime = 0
            
            // 检查是否是新的一周
            let lastWeek = calendar.component(.weekOfYear, from: lastDate)
            let currentWeek = calendar.component(.weekOfYear, from: currentDate)
            
            if lastWeek != currentWeek {
                weeklyUsageTime = 0
            }
            
            // 检查是否是新的一月
            let lastMonth = calendar.component(.month, from: lastDate)
            let currentMonth = calendar.component(.month, from: currentDate)
            
            if lastMonth != currentMonth {
                monthlyUsageTime = 0
            }
        }
    }
    
    // 保存使用数据
    private func saveUsageData() {
        let defaults = UserDefaults.standard
        defaults.set(todayUsageTime, forKey: "com.vichat.trace.todayUsageTime")
        defaults.set(weeklyUsageTime, forKey: "com.vichat.trace.weeklyUsageTime")
        defaults.set(monthlyUsageTime, forKey: "com.vichat.trace.monthlyUsageTime")
        
        if let date = lastRecordDate {
            defaults.set(date, forKey: "com.vichat.trace.lastRecordDate")
        }
    }
    
    // 加载使用数据
    private func loadUsageData() {
        let defaults = UserDefaults.standard
        todayUsageTime = defaults.double(forKey: "com.vichat.trace.todayUsageTime")
        weeklyUsageTime = defaults.double(forKey: "com.vichat.trace.weeklyUsageTime")
        monthlyUsageTime = defaults.double(forKey: "com.vichat.trace.monthlyUsageTime")
        lastRecordDate = defaults.object(forKey: "com.vichat.trace.lastRecordDate") as? Date
        
        // 检查是否需要重置统计
        if let date = lastRecordDate {
            checkAndResetStatistics(Date())
        }
    }
    
    // 获取今日使用时长
    public func getTodayUsageTime() -> TimeInterval {
        return todayUsageTime
    }
    
    // 获取本周使用时长
    public func getWeeklyUsageTime() -> TimeInterval {
        return weeklyUsageTime
    }
    
    // 获取本月使用时长
    public func getMonthlyUsageTime() -> TimeInterval {
        return monthlyUsageTime
    }
}